import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:qunaimedical/components/custom_avatar.dart';
import 'package:qunaimedical/models/moment.dart';
import 'package:qunaimedical/pages/moment/moment_detail.dart';
import 'package:qunaimedical/utils/common_util.dart';
import 'package:qunaimedical/utils/event_bus_manager.dart';
import 'package:qunaimedical/values/urls.dart';

class ItemMoment extends StatelessWidget {
  final Moment item;
  final bool clickAble;

  ItemMoment(this.item, {this.clickAble});

  @override
  Widget build(BuildContext context) {
    var gridListWidth = MediaQuery.of(context).size.width - 60;
    var images = item.images.split(',')
      ..removeWhere((element) => element == '');
    return InkWell(
      onTap: (clickAble ?? false)
          ? () {
              Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (c) => MomentDetailPage(item: item)))
                  .then((value) {
                if (value != null && value) {
                  eventBus.fire(EventBusManager(EventBusManager.momentRefresh));
                }
              });
            }
          : null,
      child: Container(
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.all(10),
                  child: ClipOval(
                    child: CustomAvatar(avatarUrl: item.avatar),
                  ),
                ),
                //姓名和时间
                Container(
                  height: 60,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(bottom: 3),
                        child: Text(
                          item.name,
                        ),
                      ),
                      Text(
                        item.created,
                        style: Theme.of(context).textTheme.caption,
                      ),
                    ],
                  ),
                ),
              ],
            ),
            //文本内容
            Offstage(
              offstage: item.content == '',
              child: Container(
                margin: EdgeInsets.only(left: 60, right: 10),
                child: Text(
                  item.content ?? '',
                  maxLines: 5,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ),
            //九空格
            Offstage(
              offstage: images.isEmpty,
              child: Container(
                margin: EdgeInsets.only(top: 10, left: 60, right: 10),
                height: images.length > 6
                    ? gridListWidth
                    : images.length > 3
                        ? gridListWidth / 3 * 2
                        : gridListWidth / 3,
                child: GridView.builder(
                  shrinkWrap: true,
                  physics: new NeverScrollableScrollPhysics(),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisSpacing: 5,
                      mainAxisSpacing: 5,
                      crossAxisCount: 3,
                      childAspectRatio: 1.0),
                  itemCount: images.length,
                  itemBuilder: (context, index) {
                    return InkWell(
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(8),
                        child: CachedNetworkImage(
                          imageUrl: '${Urls.BASE_URL}${images[index]}',
                          placeholder: (c, url) =>
                              Container(color: Colors.grey[200]),
                          errorWidget: (c, url, err) =>
                              Container(color: Colors.grey[200]),
                          fit: BoxFit.cover,
                        ),
                      ),
                      onTap: () {
                        CommonUtils.openGallery(
                            context,
                            images.map((e) => '${Urls.BASE_URL}$e').toList(),
                            index);
                      },
                    );
                  },
                ),
              ),
            ),
            SizedBox(height: 10),
            Divider(),
          ],
        ),
      ),
    );
  }
}
